﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using YArchitech.LIB;

namespace HYElectricSystemSetting
{
	public partial class WireThroughPipes : System.Windows.Forms.Form
	{
		public WireThroughPipes()
		{
			this.InitializeComponent();
			this.btnDeleteItems.Image = Resource.GetImage("delete16");
		}

		private void WireThroughPipes_Load(object sender, EventArgs e)
		{
			this.cmbSelectValue.Visible = false;
			ToolTip toolTip = new ToolTip();
			toolTip.AutoPopDelay = 5000;
			toolTip.InitialDelay = 1000;
			toolTip.ReshowDelay = 50;
			toolTip.ShowAlways = true;
			toolTip.SetToolTip(this.btnAddItems, "添加");
			toolTip.SetToolTip(this.btnDeleteItems, "删除");
			this.cmbSelectValue.DropDownStyle = ComboBoxStyle.DropDownList;
			this.cmbSelectValue.SelectedIndexChanged += this.cmbSelectValue_SelectedIndexChanged;
			this.dataGridView1.Controls.Add(this.cmbSelectValue);
			if (ElectricSystemSettingData.ConduitTypeDatas == null || ElectricSystemSettingData.WireTypeDatas == null)
			{
				return;
			}
			foreach (WireTypeData item in ElectricSystemSettingData.WireTypeDatas)
			{
				this.cmbWireType.Items.Add(item);
				this.cmbWireType.DisplayMember = "WireType";
			}
			if (this.cmbConduitType.Items.Count > 0)
			{
				this.cmbConduitType.SelectedIndex = 0;
			}
			if (this.cmbWireType.Items.Count > 0)
			{
				this.cmbWireType.SelectedIndex = 0;
			}
		}

		private void cmbSelectValue_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (ElectricSystemSettingData.CheckDatagridview(this.dataGridView1, ((ComboBox)sender).SelectedItem.ToString()))
			{
				MessageBox.Show("包含相同的值", "盈建科提示");
				((ComboBox)sender).Visible = false;
				return;
			}
			this.dataGridView1.CurrentCell.Value = Convert.ToDouble(((ComboBox)sender).SelectedItem.ToString());
			((ComboBox)sender).Visible = false;
		}

		public void AddComboxitem()
		{
			this.cmbSelectValue.Items.Clear();
			WireTypeData wireTypeData = (WireTypeData)this.cmbWireType.SelectedItem;
			foreach (WireAmpacityData wireAmpacityData in ElectricSystemSettingData.WireAmpacity)
			{
				if (wireAmpacityData.WireType == wireTypeData.Id)
				{
					foreach (KeyValuePair<double, double> keyValuePair in wireAmpacityData.AmpacityItems)
					{
						this.cmbSelectValue.Items.Add(keyValuePair.Key);
					}
				}
			}
		}

		public void AddComboxitemforConduit()
		{
			this.cmbSelectValue.Items.Clear();
			ConduitTypeSetClass conduitTypeSetClass = (ConduitTypeSetClass)this.cmbConduitType.SelectedItem;
			if (conduitTypeSetClass == null)
			{
				return;
			}
			foreach (WirePipeSizeData wirePipeSizeData in ElectricSystemSettingData.WirePipeSizeDatas)
			{
				if (conduitTypeSetClass.Id == wirePipeSizeData.WirePipeType)
				{
					foreach (double num in wirePipeSizeData.SizeItems)
					{
						this.cmbSelectValue.Items.Add(num);
					}
				}
			}
		}

		private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
		{
			if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
			{
				this.dataGridView1.CurrentCell = this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
				this.dataGridView1.BeginEdit(true);
			}
			this.cmbSelectValue.Items.Clear();
			if (this.dataGridView1.CurrentCell == null)
			{
				return;
			}
			if (this.dataGridView1.CurrentCell.ColumnIndex == 0)
			{
				this.AddComboxitem();
				if (this.dataGridView1.Rows[this.dataGridView1.CurrentCell.RowIndex].Cells[this.dataGridView1.CurrentCell.ColumnIndex].Value != null)
				{
					this.cmbSelectValue.Text = this.dataGridView1.Rows[this.dataGridView1.CurrentCell.RowIndex].Cells[this.dataGridView1.CurrentCell.ColumnIndex].Value.ToString();
				}
				Rectangle cellDisplayRectangle = this.dataGridView1.GetCellDisplayRectangle(this.dataGridView1.CurrentCell.ColumnIndex, this.dataGridView1.CurrentCell.RowIndex, false);
				this.cmbSelectValue.Left = cellDisplayRectangle.Left;
				this.cmbSelectValue.Top = cellDisplayRectangle.Top;
				this.cmbSelectValue.Width = cellDisplayRectangle.Width;
				this.cmbSelectValue.Height = cellDisplayRectangle.Height;
				this.cmbSelectValue.Visible = true;
				return;
			}
			if (this.dataGridView1.CurrentCell.ColumnIndex == 1)
			{
				this.AddComboxitemforConduit();
				if (this.dataGridView1.Rows[this.dataGridView1.CurrentCell.RowIndex].Cells[this.dataGridView1.CurrentCell.ColumnIndex].Value != null)
				{
					this.cmbSelectValue.Text = this.dataGridView1.Rows[this.dataGridView1.CurrentCell.RowIndex].Cells[this.dataGridView1.CurrentCell.ColumnIndex].Value.ToString();
				}
				Rectangle cellDisplayRectangle2 = this.dataGridView1.GetCellDisplayRectangle(this.dataGridView1.CurrentCell.ColumnIndex, this.dataGridView1.CurrentCell.RowIndex, false);
				this.cmbSelectValue.Left = cellDisplayRectangle2.Left;
				this.cmbSelectValue.Top = cellDisplayRectangle2.Top;
				this.cmbSelectValue.Width = cellDisplayRectangle2.Width;
				this.cmbSelectValue.Height = cellDisplayRectangle2.Height;
				this.cmbSelectValue.Visible = true;
				return;
			}
			this.cmbSelectValue.Visible = false;
		}

		private void toolStripButton1_Click(object sender, EventArgs e)
		{
			this.dataGridView1.Rows.Add();
		}

		private void toolStripButton2_Click(object sender, EventArgs e)
		{
			this.dataGridView1.Rows.Remove(this.dataGridView1.SelectedRows[this.dataGridView1.CurrentCell.RowIndex]);
		}

		private void cmbWireType_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.cmbSelectValue.Visible = false;
			this.cmbConduitType.Items.Clear();
			WireTypeData wireTypeData = (WireTypeData)this.cmbWireType.SelectedItem;
			foreach (string a in this.loadCmbConduitType(wireTypeData.Id))
			{
				foreach (ConduitTypeSetClass conduitTypeSetClass in ElectricSystemSettingData.ConduitTypeDatas)
				{
					if (a == conduitTypeSetClass.Id)
					{
						this.cmbConduitType.Items.Add(conduitTypeSetClass);
					}
				}
			}
			if (this.cmbConduitType.Items.Count > 0)
			{
				this.cmbConduitType.SelectedIndex = 0;
			}
			this.cmbConduitType.DisplayMember = "ConduitTypeSet";
			ConduitTypeSetClass conduitTypeSetClass2 = (ConduitTypeSetClass)this.cmbConduitType.SelectedItem;
			int num = 0;
			int num2 = 0;
			this.dataGridView1.Rows.Clear();
			foreach (WireWearPipeData wireWearPipeData in ElectricSystemSettingData.WireWearPipe)
			{
				if (conduitTypeSetClass2 != null && wireTypeData != null && wireTypeData.Id == wireWearPipeData.WireType && conduitTypeSetClass2.Id == wireWearPipeData.WirePipeType)
				{
					foreach (KeyValuePair<double, double> keyValuePair in wireWearPipeData.SizeItemsData)
					{
						this.dataGridView1.Rows.Add();
						this.dataGridView1.Rows[num].Cells[0].Value = keyValuePair.Key;
						this.dataGridView1.Rows[num].Cells[1].Value = keyValuePair.Value;
						num++;
						num2++;
					}
				}
			}
			this.dataGridView1.Sort(this.dataGridView1.Columns[0], ListSortDirection.Ascending);
		}

		public List<string> loadCmbConduitType(string wtdid)
		{
			List<string> list = new List<string>();
			foreach (WireWearPipeData wireWearPipeData in ElectricSystemSettingData.WireWearPipe)
			{
				if (wireWearPipeData.WireType == wtdid && !list.Contains(wireWearPipeData.WirePipeType))
				{
					list.Add(wireWearPipeData.WirePipeType);
				}
			}
			return list;
		}

		private void cmbConduitType_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.cmbSelectValue.Visible = false;
			this.dataGridView1.Rows.Clear();
			WireTypeData wireTypeData = (WireTypeData)this.cmbWireType.SelectedItem;
			ConduitTypeSetClass conduitTypeSetClass = (ConduitTypeSetClass)this.cmbConduitType.SelectedItem;
			int num = 0;
			int num2 = 0;
			foreach (WireWearPipeData wireWearPipeData in ElectricSystemSettingData.WireWearPipe)
			{
				if (conduitTypeSetClass != null && wireTypeData != null && conduitTypeSetClass.Id == wireWearPipeData.WirePipeType && wireTypeData.Id == wireWearPipeData.WireType)
				{
					foreach (KeyValuePair<double, double> keyValuePair in wireWearPipeData.SizeItemsData)
					{
						this.dataGridView1.Rows.Add();
						this.dataGridView1.Rows[num].Cells[0].Value = keyValuePair.Key;
						this.dataGridView1.Rows[num].Cells[1].Value = keyValuePair.Value;
						num++;
						num2++;
					}
				}
			}
			this.dataGridView1.Sort(this.dataGridView1.Columns[0], ListSortDirection.Ascending);
		}

		private void btnAddConduitType_Click(object sender, EventArgs e)
		{
			CreateWireThoroughPipeType createWireThoroughPipeType = new CreateWireThoroughPipeType(this.cmbConduitType);
			if (createWireThoroughPipeType.ShowDialog() == DialogResult.OK)
			{
				this.cmbConduitType.Items.Add(createWireThoroughPipeType.cmbBaseOn.SelectedItem);
				this.cmbConduitType.SelectedIndex = this.cmbConduitType.Items.Count - 1;
			}
			this.SaveWireWearPipe();
		}

		private void btnDeleteCT_Click(object sender, EventArgs e)
		{
			if (this.cmbConduitType.Items.Count == 1)
			{
				MessageBox.Show("至少有一种线管类型！删除失败", "盈建科提示");
				return;
			}
			if (this.cmbConduitType.SelectedItem != null && DialogResult.Yes == MessageBox.Show("是否要将选中项删除？", "盈建科提示", MessageBoxButtons.YesNo))
			{
				ConduitTypeSetClass conduitTypeSetClass = (ConduitTypeSetClass)this.cmbConduitType.SelectedItem;
				foreach (WireWearPipeData wireWearPipeData in ElectricSystemSettingData.WireWearPipe)
				{
					if (wireWearPipeData.WirePipeType == conduitTypeSetClass.Id)
					{
						ElectricSystemSettingData.WireWearPipe.Remove(wireWearPipeData);
						break;
					}
				}
				this.cmbConduitType.Items.RemoveAt(this.cmbConduitType.SelectedIndex);
				if (this.cmbConduitType.Items.Count > 0)
				{
					this.cmbConduitType.SelectedIndex = 0;
				}
			}
			this.SaveWireWearPipe();
		}

		private void cmbWireType_MouseClick(object sender, MouseEventArgs e)
		{
		}

		private void cmbConduitType_MouseClick(object sender, MouseEventArgs e)
		{
		}

		public void SaveWireWearPipe()
		{
			WireTypeData wireTypeData = (WireTypeData)this.cmbWireType.SelectedItem;
			ConduitTypeSetClass conduitTypeSetClass = (ConduitTypeSetClass)this.cmbConduitType.SelectedItem;
			if (wireTypeData == null || conduitTypeSetClass == null)
			{
				return;
			}
			bool flag = true;
			WireThroughPipes.wireThroughIsEdit = true;
			foreach (WireWearPipeData wireWearPipeData in ElectricSystemSettingData.WireWearPipe)
			{
				if (wireTypeData != null && conduitTypeSetClass != null && wireTypeData.Id == wireWearPipeData.WireType && conduitTypeSetClass.Id == wireWearPipeData.WirePipeType)
				{
					flag = false;
					wireWearPipeData.SizeItemsData.Clear();
					for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
					{
						if (this.dataGridView1.Rows[i].Cells[0].Value != null && this.dataGridView1.Rows[i].Cells[0].Value.ToString() != "" && wireWearPipeData.SizeItemsData != null && !wireWearPipeData.SizeItemsData.ContainsKey(Convert.ToDouble(this.dataGridView1.Rows[i].Cells[0].Value)))
						{
							wireWearPipeData.SizeItemsData.Add(Convert.ToDouble(this.dataGridView1.Rows[i].Cells[0].Value), Convert.ToDouble(this.dataGridView1.Rows[i].Cells[1].Value));
						}
					}
				}
			}
			if (flag)
			{
				WireWearPipeData wireWearPipeData2 = new WireWearPipeData();
				wireWearPipeData2.WireType = wireTypeData.Id;
				wireWearPipeData2.WirePipeType = conduitTypeSetClass.Id;
				Dictionary<double, double> dictionary = new Dictionary<double, double>();
				for (int j = 0; j < this.dataGridView1.Rows.Count; j++)
				{
					if (this.dataGridView1.Rows[j].Cells[0].Value != null && this.dataGridView1.Rows[j].Cells[0].Value.ToString() != "" && wireWearPipeData2.SizeItemsData != null && !wireWearPipeData2.SizeItemsData.ContainsKey(Convert.ToDouble(this.dataGridView1.Rows[j].Cells[0].Value)))
					{
						dictionary.Add(Convert.ToDouble(this.dataGridView1.Rows[j].Cells[0].Value), Convert.ToDouble(this.dataGridView1.Rows[j].Cells[1].Value));
					}
				}
				wireWearPipeData2.SizeItemsData = dictionary;
				ElectricSystemSettingData.WireWearPipe.Add(wireWearPipeData2);
			}
		}

		private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
		{
			if (this.dataGridView1.CurrentCell.ColumnIndex == 1)
			{
				try
				{
					this.dataGridView1.Sort(this.dataGridView1.Columns[0], ListSortDirection.Ascending);
				}
				catch (Exception)
				{
				}
			}
			this.SaveWireWearPipe();
		}

		private void btnAddItems_Click(object sender, EventArgs e)
		{
			if (this.dataGridView1.Rows.Count == 0)
			{
				this.dataGridView1.Rows.Add();
			}
			if (this.dataGridView1.Rows.Count > 0 && this.dataGridView1.Rows[this.dataGridView1.Rows.Count - 1].Cells[0].Value != null && this.dataGridView1.Rows[this.dataGridView1.Rows.Count - 1].Cells[0].Value.ToString() != "")
			{
				this.dataGridView1.Rows.Add();
				this.dataGridView1.ClearSelection();
				this.dataGridView1.Rows[this.dataGridView1.Rows.Count - 1].Selected = true;
			}
			this.isAddRow = true;
			this.SaveWireWearPipe();
		}

		private void btnDeleteItems_Click(object sender, EventArgs e)
		{
			if (this.dataGridView1.Rows.Count >= 1 && this.dataGridView1.SelectedRows.Count > 0)
			{
				this.dataGridView1.Rows.Remove(this.dataGridView1.SelectedRows[0]);
				this.isAddRow = true;
				this.SaveWireWearPipe();
				this.cmbSelectValue.Visible = false;
			}
		}

		private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
		{
			this.dataGridView1.FirstDisplayedScrollingRowIndex = e.RowIndex;
		}

		private void dataGridView1_CurrentCellChanged(object sender, EventArgs e)
		{
		}

		private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
		{
			this.dgvTxt = (DataGridViewTextBoxEditingControl)e.Control;
			this.dgvTxt.SelectAll();
			this.dgvTxt.KeyPress += this.Cells_KeyPress;
		}

		private void Cells_KeyPress(object sender, KeyPressEventArgs e)
		{
			ElectricSystemSettingData.keyPressXS(e, this.dgvTxt);
		}

		private void btnReName_Click(object sender, EventArgs e)
		{
		}

		private ComboBox cmbSelectValue = new ComboBox();

		public static bool wireThroughIsEdit;

		private bool isAddRow = true;

		public DataGridViewTextBoxEditingControl dgvTxt;
	}
}
